package es.daw.ejercicio3.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import es.daw.ejercicio3.dao.ContactDao;
import es.daw.ejercicio3.dao.domain.Contact;

/**
 * Servlet implementation class FindContact
 */
public class FindContact extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public FindContact() {

    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	 }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String tipoBusqueda = request.getParameter("tipoBusqueda");
		List<Contact> listaContactos = new ArrayList<Contact>();
		ContactDao contactDao = new ContactDao();

		if(tipoBusqueda.equals("rapida")){
			String busqueda = request.getParameter("search");
			listaContactos = contactDao.findContacts(busqueda);

		}else{
			Contact c = new Contact();
			c.setApellidos(request.getParameter("surname"));
			c.setCodPostal(request.getParameter("zipcode"));
			c.setDireccion(request.getParameter("address"));
			c.setNombre(request.getParameter("name"));
			c.setProvincia(request.getParameter("province"));
			c.setTelefono(request.getParameter("telephone"));

			listaContactos = contactDao.findContacts(c);


		}

		request.setAttribute("listaContactos", listaContactos);
		response.sendRedirect("index.jsp");
	}

}
